package com.fita.wechat.service.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.JsonUtils;
import com.fita.entity.agent.Agent;
import com.fita.entity.wallet.UserWallet;
import com.fita.wechat.dao.AgentDao;
import com.fita.wechat.dao.DistributionRewardRecordDao;
import com.fita.wechat.dao.UserWalletDao;
import com.fita.wechat.rao.ApiRao;
import com.fita.wechat.service.UserWalletService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: 11200
 * @date: 2019/7/1
 */
@Service("userWalletService")
public class UserWalletServiceImpl extends DataServiceImpl<UserWallet> implements UserWalletService {

    private static final Logger logger = LoggerFactory.getLogger(UserWalletServiceImpl.class);

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Dao
    private DistributionRewardRecordDao distributionRewardRecordDao;

    @Dao
    private UserWalletDao userWalletDao;

    @Dao
    private AgentDao agentDao;

    @Rao
    private ApiRao apiRao;

    @Value("#{ctx['api.host']}")
    private String apiHost;

    /**
     * 获取用户钱包，如果为空则创建
     * @param mobile
     * @return
     */
    public UserWallet createOrLoadByMobile(String mobile) {

        UserWallet userWallet = userWalletDao.loadByMobile(mobile);

        return userWallet;
    }

    /**
     * 计算冻结金额
     *
     * @param agent
     * @return
     */
    public int calculateFreezeDistributionRewardAmount(Agent agent) {

        UserWallet userWallet = userWalletDao.loadByMobile(agent.getMobile());

        int freezeDistributionRewardAmount = distributionRewardRecordDao.calculateFreezeDistributionRewardAmount(userWallet);

        return freezeDistributionRewardAmount;
    }

    /**
     * 提现
     *
     * @param id
     * @param amount
     */
    public void withdraw(Long id, Integer amount) {

        Agent agent = genericDaoSupport.load(Agent.class, id);

        if (amount < 100) {
            throw new BusinessException("500", "提现金额小于100");
        }
        amount = amount * 100;

        HttpResult httpResult = null;

        try {
            httpResult = apiRao.withdraw(apiHost + "/wallet/withdraw", agent.getMobile(), amount);

        } catch (Exception e) {

            logger.error("#withdarw() call remote withdraw api error [{}]", e);

            throw new BusinessException("500","提现失败");
        }
        logger.info("#withdarw() call remote withdraw api success. the response value is [{}]", JsonUtils.toJson(httpResult));

        if (!httpResult.isValid()){
            throw new BusinessException("500", httpResult.getMessage());


        }

    }
}
